﻿
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;

namespace SoonCode.FocusTest.ViewModels
{
    public class AnswerItemHViewModel : BindableBase
    {
        private IEventAggregator aggregator;
        private string testType = "itemH";
        private ObservableCollection<HItemHelper> sourceList;
        private int currTime = 0;
        private int tabIndex = 0;
        private Visibility threeAge = Visibility.Visible;
        private Visibility fourAge = Visibility.Visible;
        private Visibility fiveAge = Visibility.Visible;

        public Visibility FiveAge
        {
            get
            {
                return fiveAge;
            }

            set
            {
                fiveAge = value;
                RaisePropertyChanged();
            }
        }

        public Visibility FourAge
        {
            get
            {
                return fourAge;
            }
            set
            {
                fourAge = value;
                RaisePropertyChanged();
            }
        }
        public Visibility ThreeAge
        {
            get
            {
                return threeAge;
            }
            set
            {
                threeAge = value;
                RaisePropertyChanged();
            }
        }


        public int TabIndex
        {
            get { return tabIndex; }
            set
            {
                tabIndex = value;
                RaisePropertyChanged();
            }
        }

        public int CurrTime
        {
            get { return currTime; }
            set
            {
                currTime = value;
                RaisePropertyChanged();
            }
        }
        public ICommand AnswerCommand { get; set; }


        public ObservableCollection<HItemHelper> SourceList
        {
            get { return sourceList; }
            set
            {
                sourceList = value;
                RaisePropertyChanged();
            }
        }
        public AnswerItemHViewModel(IEventAggregator aggregator)
        {
            AnswerCommand = new DelegateCommand<string>(StudentAnswer);
            this.aggregator = aggregator;
            this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);
            this.aggregator.GetEvent<StudentStartTestEvent>().Subscribe(OnStart);
            this.aggregator.GetEvent<QuestionChangeEvent>().Subscribe(OnQuestionChange);
            this.aggregator.GetEvent<ChangeTestEvent>().Subscribe(TestChange);
        }

        private void TestChange(int p)
        {
            TabIndex = 0;
        }

        private void OnQuestionChange(EventInfo info)
        {
            if (info.Type != testType) return;
            CurrTime = (int)info.Data["currTime"];
        }
        private void OnStart(EventInfo info)
        {
            if (info.Type != testType) return;
            SourceList = (ObservableCollection<HItemHelper>)info.Data["source"];
            TabIndex = 1;
        }
        private void OnLoaded(int p)
        {
            if (p != 7) return;
            TabIndex = 0;
            int age = GlobalModel.Instance.MemberInfo.age;
            ThreeAge = age >= 3 ? Visibility.Visible : Visibility.Collapsed;
            FourAge = age >= 4 ? Visibility.Visible : Visibility.Collapsed;
            FiveAge = age >= 5 ? Visibility.Visible : Visibility.Collapsed;
        }

        public void StudentAnswer(string p)
        {
            SoundPlayer.PlayButton();
            EventInfo info = new EventInfo(testType);
            info.Data["index"] = p;
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(info);
        }

    }
}
